package com.ssh.manager.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.SuperMarket;
import com.ssh.entity.SuperMarketProduct;
import com.ssh.manager.SuperMarketManager;

@Service
public class SuperMarketManagerImpl implements SuperMarketManager {
	@Resource
	private IDao daoImpl;
	
	@Override
	public List<SuperMarket> findAll() {
		String sql = "from SuperMarket where del!=1 order by createDate desc";
		List<SuperMarket> superMarkets = (List<SuperMarket>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, 0, -1);
		return superMarkets;
	}

	@Override
	public boolean delete(Long id) {
		try {
			SuperMarket s = daoImpl.getDomain(SuperMarket.class, id);
			s.setDel(1);
			daoImpl.saveOrUpdateDomain(s);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public void findListByBrand(Long brandId, Page page) {
		String sql = "select count(*) from SuperMarketProduct where del!=1 and product.brand.id=? group by superMarket.id";
		String findSql = "from SuperMarketProduct where del!=1 and product.brand.id=? group by superMarket.id";
		int sum = daoImpl.countAll(sql, new Object[]{brandId});
		page.setTotalCount(new Long(sum));
		page.countTotalPageNum();
		List<SuperMarketProduct> sp = (List<SuperMarketProduct>)daoImpl.findListBySql(findSql, new Object[]{brandId}, false, true, false, 0, -1);
		List<SuperMarket> superMarkets = new ArrayList<SuperMarket>();
		for (int i = 0; i < sp.size(); i++) {
			superMarkets.add(sp.get(i).getSuperMarket());
		}
		page.setPageData(superMarkets);
	}

	@Override
	public void findListByBrandIds(String[] brandIds, Page page) {
		String sql = "from SuperMarketProduct where del!=1 and product.id in(select id from Product where del!=1";
		for (int i = 0; i < brandIds.length; i++) {
			sql += " or brand.id="+brandIds[i];
		}
		sql += ") group by superMarket.id";
		
		sql = sql.replaceFirst("or", "and");
		
		List<SuperMarketProduct> sum = (List<SuperMarketProduct>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, 0, -1);
		page.setTotalCount(new Long(sum.size()));
		page.countTotalPageNum();
		
		List<SuperMarketProduct> superMarketProducts = (List<SuperMarketProduct>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		List<SuperMarket> superMarkets = new ArrayList<SuperMarket>();
		for (int i = 0; i < superMarketProducts.size(); i++) {
			superMarkets.add(superMarketProducts.get(i).getSuperMarket());
		}
		page.setPageData(superMarkets);
	}
	
	@Override
	public List<SuperMarket> findMarketsByCIDAll(Long companyId) {
		String sql = "from SuperMarket where company.id=? and del!=1 order by createDate desc";
		List<SuperMarket> superMarkets = (List<SuperMarket>)daoImpl.findListBySql(sql, new Object[]{companyId}, false, true, false, 0, -1);
		return superMarkets;
	}

	@Override
	public SuperMarket getSuperMarketById(Long id) {
		return daoImpl.getDomain(SuperMarket.class, id);
	}

	@Override
	public SuperMarket updateSuperMarket(SuperMarket superMarket) {
		return daoImpl.saveOrUpdateDomain(superMarket);
	}

	@Override
	public SuperMarket addSuperMarket(SuperMarket superMarket) {
		return daoImpl.saveDomain(superMarket);
	}

	@Override
	public List<SuperMarket> superMarketList(Long companyId, long employeeId) {
		// TODO Auto-generated method stub
		return  (List<SuperMarket>)daoImpl.findListBySql("from  SuperMarket where company.id=? and employee.id=? and del=?  and employee.del=0 and company.del=0 order by createDate desc", new Object[]{companyId,employeeId,0}, false, true, false, 0, -1);
		
	}

	@Override
	public void findSuperMarketsByCID(Long companyId, Page page,
			String isDesc) {
		String sql = "select count(*) from SuperMarket where  del!=1";
		String findSql = "from SuperMarket where del!=1";
		if(companyId!=null){
			sql += " and company.id="+companyId;
			findSql += " and company.id="+companyId;
		}
		findSql += " order by createDate "+isDesc;
		try {
			int companySum = daoImpl.countAll(sql, null);
			page.setTotalCount(new Long(companySum));
			page.countTotalPageNum();
		} catch (Exception e) {
			LogFactory.getDefaultLog().error(e.getLocalizedMessage(), LogName.API);
		}
		List<SuperMarket> superMarkets = (List<SuperMarket>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		page.setPageData(superMarkets);
	}

}
